﻿namespace Microsoft.SharePoint.Client
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;

    [ScriptType("SP.ViewCollection", ServerTypeId="{03c5d7a9-9541-4482-9919-ca0cccf565a0}")]
    public class ViewCollection : ClientObjectCollection<View>
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        public ViewCollection(ClientRuntimeContext Context, ObjectPath ObjectPath) : base(Context, ObjectPath)
        {
        }

        [Remote]
        public View Add(ViewCreationInformation parameters)
        {
            if (base.Context.ValidateOnClient)
            {
                if (parameters == null)
                {
                    throw ClientUtility.CreateArgumentNullException("parameters");
                }
                if (parameters != null)
                {
                    if ((parameters.Title != null) && (parameters.Title.Length > 0xff))
                    {
                        throw ClientUtility.CreateArgumentException("parameters.Title");
                    }
                    if (parameters.RowLimit > 0x7fffffff)
                    {
                        throw ClientUtility.CreateArgumentException("parameters.RowLimit");
                    }
                }
            }
            View view = new View(base.Context, new ObjectPathMethod(base.Context, base.Path, "Add", new object[] { parameters }));
            view.Path.SetPendingReplace();
            ObjectIdentityQuery query = new ObjectIdentityQuery(view.Path);
            base.Context.AddQueryIdAndResultObject(query.Id, view);
            base.Context.AddQuery(query);
            base.AddChild(view);
            view.InitFromCreationInformation(parameters);
            return view;
        }

        [Remote]
        public View GetById(Guid guidId)
        {
            Dictionary<Guid, View> dictionary;
            object obj2;
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetById", out obj2))
            {
                dictionary = (Dictionary<Guid, View>) obj2;
            }
            else
            {
                dictionary = new Dictionary<Guid, View>();
                base.ObjectData.MethodReturnObjects["GetById"] = dictionary;
            }
            View view = null;
            if (!dictionary.TryGetValue(guidId, out view))
            {
                view = new View(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetById", new object[] { guidId }));
                dictionary[guidId] = view;
            }
            return view;
        }

        [Remote]
        public View GetByTitle(string strTitle)
        {
            Dictionary<string, View> dictionary;
            object obj2;
            if (base.Context.ValidateOnClient && (strTitle == null))
            {
                throw ClientUtility.CreateArgumentNullException("strTitle");
            }
            if (base.ObjectData.MethodReturnObjects.TryGetValue("GetByTitle", out obj2))
            {
                dictionary = (Dictionary<string, View>) obj2;
            }
            else
            {
                dictionary = new Dictionary<string, View>();
                base.ObjectData.MethodReturnObjects["GetByTitle"] = dictionary;
            }
            View view = null;
            if (!dictionary.TryGetValue(strTitle, out view))
            {
                view = new View(base.Context, new ObjectPathMethod(base.Context, base.Path, "GetByTitle", new object[] { strTitle }));
                dictionary[strTitle] = view;
            }
            return view;
        }

        protected override bool InitOnePropertyFromJson(string peekedName, JsonReader reader)
        {
            bool flag = base.InitOnePropertyFromJson(peekedName, reader);
            if (flag)
            {
                return flag;
            }
            return flag;
        }
    }
}

